/*
 * @lc app=leetcode.cn id=1508 lang=javascript
 *
 * [1508] 子数组和排序后的区间和
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} n
 * @param {number} left
 * @param {number} right
 * @return {number}
 */
var rangeSum = function(nums, n, left, right) {
  var sums = [];
  for (let i = 0; i < n; i++) {
    sums.push(nums[i]);
    let tmp = nums[i];
    for (let j = i+1; j < n; j++) {
      tmp = tmp + nums[j];
      sums.push(tmp);
    }
  }

  var sSums = sums.sort((a,b)=> a-b);
  let sum  = 0;
  for (let i = left; i <= right; i++) { 
    sum = (sum + sSums[i - 1]) % 1000000007;
  } 
  return sum;
};
// @lc code=end

